/*
 * @Author: wfl
 * @LastEditors: wfl
 * @description:
 * @updateInfo: 本次更新内容：
 * @Date: 2021-12-08 13:55:24
 * @LastEditTime: 2021-12-09 14:27:56
 */
import * as Cesium from 'cesium';
import { Property } from 'cesium';
// st.t 垂直方向
// st.s 水平方向
// fract((st.s + time * 2.0)  顺时针
// fract(st.s - time * 2.0)  逆时针
// 着色器代码  materialInput.st * 2.0   2.0代表重复的数量
const source = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
{\n\
    czm_material material = czm_getDefaultMaterial(materialInput);\n\
    vec2 st = materialInput.st * 2.0;\n\
    vec4 colorImage = texture2D(image, vec2(fract(st.s - time * 2.0), st.t));\n\
    material.alpha = colorImage.a * color.a;\n\
    material.diffuse = color.rgb;\n\
    return material;\n\
}"
function PolylineTrailLinkMaterialProperty(color: any, duration: any): any {
  this._definitionChanged = new Cesium.Event();
  this._color = undefined;
  this._colorSubscription = undefined;
  this.color = color;
  this.duration = duration;
  this._time = (new Date()).getTime();

  // 定义source 达到动态设置glsl的目的
}

Object.defineProperties(PolylineTrailLinkMaterialProperty.prototype, {
  isConstant: {
    get: function () {
      return false;
    }
  },
  definitionChanged: {
    get: function () {
      return this._definitionChanged;
    }
  },
  color: Cesium.createPropertyDescriptor('color')
});

PolylineTrailLinkMaterialProperty.prototype.getType = function (time: any) {
  return 'PolylineTrailLink';
}
PolylineTrailLinkMaterialProperty.prototype.getValue = function (time: any, result: any) {
  console.log('time: ', time);
  if (!Cesium.defined(result)) {
    result = {};
  }
  result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
  result.image = Cesium.Material.PolylineTrailLinkImage;
  result.time = (((new Date()).getTime() - this._time) % this.duration) / this.duration;
  return result;
}
PolylineTrailLinkMaterialProperty.prototype.equals = function (other: any) {
  return this === other || (other instanceof PolylineTrailLinkMaterialProperty && Property.equals(this._color, other._color))
};

console.log(Cesium);
Cesium.Material.PolylineTrailLinkType = 'PolylineTrailLink';
Cesium.Material.PolylineTrailLinkImage = "/cesiumAssets/img/99.png"//"/cesiumAssets/img/arrow.png";//图片
Cesium.Material.PolylineTrailLinkSource = source;

Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
  fabric: {
    type: Cesium.Material.PolylineTrailLinkType,
    uniforms: {
      color: Cesium.Color.WHITE,
      image: Cesium.Material.PolylineTrailLinkImage,
      time: 0,
      repeat: new Cesium.Cartesian2(50, 1)
    },
    // components: {
    //   diffuse: "texture2D(image, fract(repeat * materialInput.st - time)).rgb * color.rgb",
    //   alpha: "texture2D(image, vec2(fract(repeat * materialInput.st - time))).a",
    //   // normal: "vec3(0.0, 0.0, 1.0)"
    // },
    // texture2D(image, fract(repeat * materialInput.st - time)).rgb * color.rgb
    // texture2D(image, vec2(fract(st.s - time), st.t))
    source: Cesium.Material.PolylineTrailLinkSource,
  },
  translucent: function (material) {
    return true;
  }
})

export default PolylineTrailLinkMaterialProperty